package com.xcx.services.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.xcx.bo.BoxListInfo;
import com.xcx.bo.BoxListInfoExample;
import com.xcx.bo.BoxLogSceneOperation;
import com.xcx.common.dto.base.BaseDto;
import com.xcx.common.dto.base.BaseReq;
import com.xcx.common.dto.base.BaseResp;
import com.xcx.common.dto.req.SaveUserInfoReq;
import com.xcx.common.dto.req.UserLoginReq;
import com.xcx.common.dto.resp.UserLoginResp;
import com.xcx.common.enums.CommConfigParamKeyEnum;
import com.xcx.common.enums.IsValidEnum;
import com.xcx.common.services.BoxService;
import com.xcx.common.services.common.CommConfigService;
import com.xcx.common.services.common.CommUserService;
import com.xcx.common.utils.CheckParamUtils;
import com.xcx.common.utils.ExceptionUtils;
import com.xcx.common.utils.IdGenUtils;
import com.xcx.common.utils.StringUtils;
import com.xcx.dao.BoxListInfoDao;
import com.xcx.dao.BoxLogSceneOperationDao;
import com.xcx.dto.QueryBoxIsShowResp;
import com.xcx.dto.QueryBoxListResp;
import com.xcx.dto.QuerySharePhotoResp;
import com.xcx.dto.RecordSceneReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;

@Slf4j
@Service
public class BoxServiceImpl implements BoxService {

    @Reference
    private CommUserService commUserService;

    @Reference
    private CommConfigService commConfigService;

    @Autowired
    private BoxListInfoDao boxListInfoDao;

    @Autowired
    private BoxLogSceneOperationDao boxLogSceneOperationDao;

    @Value("${wx.boxAppId}")
    private String wxAppId;

    @Value("${wx.boxAppSecret}")
    private String wxAppSecret;

    /**
     * 用户登录
     *
     * @param reqStr
     * @return
     */
    @Override
    public String userLogin(String reqStr) {
        log.info("userLogin.req:{}", reqStr);
        UserLoginResp UserLoginResp = new UserLoginResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            UserLoginReq userLoginReq = BaseDto.fromJson(reqStr, UserLoginReq.class);
            userLoginReq.setAppId(wxAppId);
            userLoginReq.setSecret(wxAppSecret);
            CheckParamUtils.validateReqGson(userLoginReq);
            UserLoginResp = commUserService.userLogin(userLoginReq);
            if (UserLoginResp.isSuccess()) {
            	UserLoginResp.buildDefaultSuccessResp(userLoginReq);
			}
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("BoxServiceImpl", reqStr, UserLoginResp, e.getMessage(), e);
        }
        log.info("userLogin.resp:{}", UserLoginResp.toString());
        return UserLoginResp.toString();
    }

    /**
     * 保存用户信息
     *
     * @param reqStr
     * @return
     */
    @Override
    public String saveUserInfo(String reqStr) {
        log.info("saveUserInfo.req:{}", reqStr);
        BaseResp baseResp = new BaseResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            SaveUserInfoReq saveUserInfoReq = BaseDto.fromJson(reqStr, SaveUserInfoReq.class);
            CheckParamUtils.validateReqGson(saveUserInfoReq);
            baseResp = commUserService.saveUserInfo(saveUserInfoReq);
            if (baseResp.isSuccess()) {
            	baseResp.buildDefaultSuccessResp(saveUserInfoReq);
			}
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("BoxServiceImpl", reqStr, baseResp, e.getMessage(), e);
        }
        log.info("saveUserInfo.resp:{}", baseResp.toString());
        return baseResp.toString();
    }

    /**
     * 查询列表
     *
     * @param reqStr
     * @return
     */
    @Override
    public String queryBoxList(String reqStr) {

        log.info("queryBoxList.req:{}", reqStr);
        QueryBoxListResp baseResp = new QueryBoxListResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            BaseReq baseReq = BaseDto.fromJson(reqStr, BaseReq.class);
            CheckParamUtils.validateReqGson(baseReq);
            BoxListInfoExample example = new BoxListInfoExample();
            example.setOrderByClause("product_sort");
            example.createCriteria().andProductStatusEqualTo(IsValidEnum.VALID_YES.getCode());
            List<BoxListInfo> lst = boxListInfoDao.selectByExample(example);

            baseResp.buildDefaultSuccessResp(baseReq);
            baseResp.setBoxListInfoList(lst);
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("BoxServiceImpl", reqStr, baseResp, e.getMessage(), e);
        }
        log.info("queryBoxList.resp:{}", baseResp.toString());
        return baseResp.toString();
    }

    /**
     * 保存什么地方跳到盒子的
     *
     * @param reqStr
     * @return
     */
    @Override
    public String saveScenceLog(String reqStr) {
        log.info("saveScenceLog.req:{}", reqStr);
        BaseResp baseResp = new BaseResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            RecordSceneReq recordSceneReq = BaseDto.fromJson(reqStr, RecordSceneReq.class);
            CheckParamUtils.validateReqGson(recordSceneReq);
            BoxLogSceneOperation boxLogSceneOperation = new BoxLogSceneOperation();
            boxLogSceneOperation.setOperationNo(IdGenUtils.uuid());
            boxLogSceneOperation.setUserNo(recordSceneReq.getUserNo());
            boxLogSceneOperation.setSceneType(recordSceneReq.getSceneType());
            boxLogSceneOperation.setProductCode(recordSceneReq.getProductCode());
            boxLogSceneOperation.setDeviceInfo(recordSceneReq.getDeviceInfo());
            boxLogSceneOperationDao.insertSelective(boxLogSceneOperation);

            baseResp.buildDefaultSuccessResp(recordSceneReq);
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("BoxServiceImpl", reqStr, baseResp, e.getMessage(), e);
        }
        log.info("saveScenceLog.resp:{}", baseResp.toString());
        return baseResp.toString();
    }

    /**
     * 通过对应产品的产品号判断是否显示盒子入口
     *
     * @param reqStr
     * @return
     */
    @Override
    public String isShowBox(String reqStr) {
        log.info("isShowBox.req:{}", reqStr);
        QueryBoxIsShowResp baseResp = new QueryBoxIsShowResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            BaseReq baseReq = BaseDto.fromJson(reqStr, BaseReq.class);
            CheckParamUtils.validateReqGson(baseReq);
            String isShow = commConfigService.queryParamValue(CommConfigParamKeyEnum.BOX_IS_SHOW);

            baseResp.buildDefaultSuccessResp(baseReq);
            baseResp.setIsShow(isShow);
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("BoxServiceImpl", reqStr, baseResp, e.getMessage(), e);
        }
        log.info("isShowBox.resp:{}", baseResp.toString());
        return baseResp.toString();
    }

    /**
     * 查询分享图片列表
     *
     * @param reqStr
     * @return
     */
    @Override
    public String querySharePhoto(String reqStr) {
        log.info("querySharePhoto.req:{}", reqStr);
        QuerySharePhotoResp baseResp = new QuerySharePhotoResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            BaseReq baseReq = BaseDto.fromJson(reqStr, BaseReq.class);
            CheckParamUtils.validateReqGson(baseReq);
            String photoPathStr = commConfigService.queryParamValue(CommConfigParamKeyEnum.BOX_SHARE_PHOTO);
            if (StringUtils.isNotBlank(photoPathStr)) {
                JSONArray array = new JSONArray();
                String[] strs = photoPathStr.split(";");
                for (String str : strs) {
                    array.add(str);
                }
                baseResp.setPhotoPaths(array.toJSONString());
            }

            baseResp.buildDefaultSuccessResp(baseReq);
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("BoxServiceImpl", reqStr, baseResp, e.getMessage(), e);
        }
        log.info("querySharePhoto.resp:{}", baseResp.toString());
        return baseResp.toString();
    }
}
